<template>
  <div class="login_container">
    <div class="lgog">
      <img src="../assets/images/logo.png"
           alt="">
    </div>
    <div class="login_box">
      <div class="login_img">
        <img src="../assets/logo.png"
             alt="">
      </div>

      <el-form label-width="80px"
               :model="loginForm"
               :rules="fromRules"
               ref="formRef">
        <div class="ipt">
          <el-form-item label="用户名"
                        prop="username">
            <el-input prefix-icon="iconfont icon-yonghu1"
                      v-model="loginForm.username"></el-input>
          </el-form-item>

          <el-form-item label="密码"
                        prop="password">
            <el-input prefix-icon="iconfont icon-mimasuo"
                      v-model="loginForm.password"></el-input>
          </el-form-item>
        </div>

        <el-form-item class="btn">
          <el-button type="success"
                     size="medium"
                     @click="senForm"
                     round>登录</el-button>
          <el-button type="warning"
                     size="medium"
                     round
                     @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // uanme必须和loginForm中的username名称一致
      fromRules: {
        username: [
          { required: true, message: '请输入登录名称', trgger: 'blur' },
          { min: 3, max: 10, message: '长度在3 到 10个字符', trgger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trgger: 'blur' },
          { min: 6, max: 15, message: '密码长度在6-15个字符', trgger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reset () {
      // ref="formRef"能拿到这个表单对象
      this.$refs.formRef.resetFields()
    },
    // validate是element官方的方法，能对表单验证，返回值是一个布尔值
    senForm () {
      this.$refs.formRef.validate(async bool => {
        if (!bool) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        console.log(res)
        if (res.meta.status !== 200) return this.$message.error('登录失败，请重新登录！')
        this.$message.success('登录成功！')
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background: rgb(255, 255, 255);
  background: linear-gradient(
    180deg,
    rgba(255, 255, 255, 1) 0%,
    rgba(255, 255, 255, 1) 30%,
    rgb(175, 183, 189) 100%
  );
  height: 100%;
}
.lgog {
  width: 300px;
  img {
    width: 100%;
    height: 120px;
  }
}
.login_box {
  position: relative;
  width: 450px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 8px;
  box-shadow: 0px 1px 22px 3px rgba(0, 0, 0, 0.5);
  .login_img {
    width: 120px;
    height: 120px;
    border: 5px solid #fff;
    margin: 5px;
    position: absolute;
    top: -60px;
    left: 50%;
    transform: translateX(-50%);
    background-color: #f0f0f0;
    border-radius: 50%;
    overflow: hidden;
    box-shadow: 0px -1px 6px 2px rgba(0, 0, 0, 0.5);
  }
  .ipt {
    width: 400px;
    position: absolute;
    top: 100px;
    left: 10px;
  }
  .btn {
    position: absolute;
    bottom: 0;
    right: 60px;
  }
}

.login_img > img {
  width: 100%;
}
</style>
